
package params;
import java.io.IOException;
import java.util.LinkedList;

import org.xml.sax.SAXException; 
import javax.xml.parsers.*;
//import java.lang.Object.*;
import org.w3c.dom.*;

/**
 * Read parameters from XML file and parse them into a byte-array list
 */
class XmlDomParser{
    /**
     * Parses the XML file with a given filename, -link http://download.oracle.com/javase/1.4.2/docs/api/javax/xml/parsers/ParserConfigurationException.html is thrown
     * @param fileName - name of XML file
     * @return list of byte arrays that contain parsed parameters in specified order
     */
    public LinkedList<byte[]> parseXmlFile(String fileName) throws ParserConfigurationException, SAXException, IOException{
        //get the factory        
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
        	LinkedList<byte[]> res = new LinkedList<byte[]>();
            //Using factory get an instance of document builder
            DocumentBuilder db = dbf.newDocumentBuilder();

            //parse using builder to get DOM representation of the XML file
            Document dom = db.parse(fileName);

            Element docEle = dom.getDocumentElement();

            //get a nodelist of  elements
            NodeList nl = docEle.getElementsByTagName("Constant");
            if(nl != null && nl.getLength() > 0) {
            	res.add(getValue(nl,"INDENT"));  
            	res.add(getValue(nl,"INDENT_SYMBOL"));                        
            	res.add(getValue(nl,"INDENT_STARTER"));  
            	res.add(getValue(nl,"INDENT_FINISHER"));
            	res.add(getValue(nl,"END_OF_LINE"));  
            	res.add(getValue(nl,"LINE_FEED"));  
            	res.add(getValue(nl,"SPACE"));  
            	res.add(getValue(nl,"COMMENTARY_PART_1"));  
            	res.add(getValue(nl,"COMMENTARY_PART_2"));  
            }   
            return res;
        }catch(ParserConfigurationException pce) {
               throw pce;
        }catch(SAXException se) {
                throw se;
        }catch(IOException ioe) {
                throw ioe;
        }
    }
    
    private byte[] getValue(NodeList nl, String name) {
        Element el = null;
        boolean found = false;
        String str1 = null;
        for(int i = 0 ; i < nl.getLength();i++) {
            str1= getTextValue((Element)nl.item(i),"Name",0);
            if( str1.equals(name)){
                found = true;
                el = (Element)nl.item(i);
                break;
            }
        }
        if(!found){
            return null;
        }
        int j = 0;
        String str = getTextValue(el,"Value",0);
        byte[] b = new byte[getNodeListLen(el,"Value")];
        while(str != null){
            if(el.getAttribute("type").equals("number")){
                b[j] = (byte)Integer.parseInt(str);
            } 
            else {
                b[j] = (byte) str.charAt(0);
            }    
            j ++;
            str = getTextValue(el,"Value",j);
        }
        return b;
    }
    
    private int getNodeListLen(Element ele, String tagName){
        return ele.getElementsByTagName(tagName).getLength();
    }
    
    private String getTextValue(Element ele, String tagName, int i) {
            String textVal = null;
            NodeList nl = ele.getElementsByTagName(tagName);
            if(nl != null && nl.getLength() > 0) {
                Element el = (Element)nl.item(i);
                if(el != null){
                    textVal = el.getFirstChild().getNodeValue();
                }
            }

            return textVal;
    }
}
